import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/Login',
    name: 'login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/',
    name: 'home',
    component: HomeView,
    // redirect: '/about',//页面默认加载的路由
    meta: {
      title: '首页',
      path: "/"
    },
    children: [
      {
        path: '/DeptManage',
        component: () => import('../components/DeptManage.vue'),
        meta: {
          title: '部门管理',
          path: "/DeptManage"
        }
      },
      {
        path: '/Department', component: () => import('../components/Department.vue'),
        meta: {
          title: '科室管理',
          path: "/Department"
        }
      },
      {
        path: '/Bodyparts', component: () => import('../components/Bodyparts.vue'),
        meta: {
          title: '身体部位',
          path: "/Bodyparts"
        }
      },
      {
        path: '/Concomitants', component: () => import('../components/Concomitants.vue'),
        meta: {
          title: '症状',
          path: "/Concomitants"
        }
      },
      {
        path: '/FoordataManage', component: () => import('../components/FoordataManage.vue'),
        meta: {
          title: '楼层管理',
          path: "/FoordataManage"
        }
      },
      {
        path: '/DoctorManage', component: () => import('../components/DoctorManage.vue'),
        meta: {
          title: '专家管理',
          path: "/DoctorManage"
        }
      },
    ]
  },
  // {
  //   path: '/Department',
  //   name: 'Department',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../components/Department.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 导航守卫					
// 使用 router.beforeEach(进入路由之前) 注册一个全局前置守卫，判断用户是否登陆					
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') {
    next();
  } else {
    let token = sessionStorage.getItem('token');
    console.log(token);
    if (token === null || token === '') {
      console.log(token);
      next('/Login');
    } else {
      console.log("dskahdjksadsa");
      next();
    }
  }
});

export default router
